import { reqUserAddress,reqOrderInfo,reqSubmitOrder} from "@/api";
const actions = {
  async getUserAddress({commit}){
    let res = await reqUserAddress();
    if(res.code == 200){
      commit('USER_ADDRESS',res.data);
    }
  },
  async getOrderInfo({commit}){
    let res = await reqOrderInfo();
    if(res.code == 200){
      commit('ORDER_INFO',res.data);
    }
  },
  async getSubmitOrder({commit},{tradeNo,data}){
    let res = await reqSubmitOrder(tradeNo,data);
    if(res.code == 200){
      commit('SUBSCRIBE_ORDER',res.data);
      return 'OK';
    }else {
      return Promise.reject(new Error(res.message));
    }
  }
};
const mutations = {
  USER_ADDRESS(state,address){
    state.address = address;
  },
  ORDER_INFO(state,orderInfo){
    state.orderInfo = orderInfo;
  },
  SUBSCRIBE_ORDER(state,orderId){
    state.orderId = orderId
  }
};
const state = {
  address:[],
  orderInfo:{},
  orderId:'',
};
const getters = {
  detailArrayList(state){
    return state.orderInfo.detailArrayList || [];
  }
};
export default {
  namespaced:true,
  actions,
  mutations,
  state,
  getters,
}